﻿using System.Net.Http;

namespace Coding4Fun.Maelstrom.Common
{
	public sealed class HardwareCommunication
	{
		public void Score(Player winner, Player loser, double normalizedHealth)
		{
			if(!Settings.UseHardware)
				return;

			var uriScore = UrlHelper.Score(winner);
			var uriLose = UrlHelper.Damage(loser, normalizedHealth);	

			var clientWinner = new HttpClient();
			var clientLoser = new HttpClient();

			clientWinner.GetAsync(uriScore, HttpCompletionOption.ResponseHeadersRead);
			clientLoser.GetAsync(uriLose, HttpCompletionOption.ResponseHeadersRead);
		}

		public void StartGame()
		{
			if(!Settings.UseHardware)
				return;

			var uriP1 = UrlHelper.GameStartedState(Player.One);
			var uriP2 = UrlHelper.GameStartedState(Player.One);

			var clientPlayer1 = new HttpClient();
			var clientPlayer2 = new HttpClient();

			clientPlayer1.GetAsync(uriP1, HttpCompletionOption.ResponseHeadersRead);
			clientPlayer2.GetAsync(uriP2, HttpCompletionOption.ResponseHeadersRead);
		}

		public void EndGame(Player winner, Player loser)
		{
			if(!Settings.UseHardware)
				return;

			var uriWin = UrlHelper.WinningState(winner);
			var uriLose = UrlHelper.LosingState(loser);

			var clientWinner = new HttpClient();
			var clientLoser = new HttpClient();

			clientWinner.GetAsync(uriWin, HttpCompletionOption.ResponseHeadersRead);
			clientLoser.GetAsync(uriLose, HttpCompletionOption.ResponseHeadersRead);
		}

		public void WaitingRoom()
		{
			if(!Settings.UseHardware)
				return;

			var uriPlayer1 = UrlHelper.SetGameStateUrl(Player.One, GameStateAnimation.Idle);
			var uriPlayer2 = UrlHelper.SetGameStateUrl(Player.Two, GameStateAnimation.Idle);

			var clientPlayer1 = new HttpClient();
			var clientPlayer2 = new HttpClient();

			clientPlayer1.GetAsync(uriPlayer1, HttpCompletionOption.ResponseHeadersRead);
			clientPlayer2.GetAsync(uriPlayer2, HttpCompletionOption.ResponseHeadersRead);
		}
	}
}
